//
//  LPDoubleComponentPickerViewController.m
//  Pickers
//
//  Created by johnny tang on 7/25/13.
//  Copyright (c) 2013 TOSC. All rights reserved.
//

#import "LPDoubleComponentPickerViewController.h"

@interface LPDoubleComponentPickerViewController ()

@end

@implementation LPDoubleComponentPickerViewController

@synthesize doublePicker;
@synthesize fillingTypes;
@synthesize breadTypes;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSArray *fillingArray = [[NSArray alloc] initWithObjects:@"Ham",
                             @"Turkey", @"Peanut Butter", @"Tuna Salad",
                             @"Chicken Salad", @"Roast Beef", @"Vegemite", nil];
    self.fillingTypes = fillingArray;
    NSArray *breadArray = [[NSArray alloc] initWithObjects:@"White", @"Whole Wheat", @"Rye", @"Sourdough", @"Seven Grain", nil];
    self.breadTypes = breadArray;
}

- (void) viewDidUnload {
    
    self.doublePicker = nil;
    self.breadTypes = nil;
    self.fillingTypes = nil;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)buttonPressed:(id)sender {
    
    NSInteger breadTypeRow = [self.doublePicker selectedRowInComponent:kBreadComponent];
    NSInteger fillingTypeRow = [self.doublePicker selectedRowInComponent:kFillingComponent];
    
    NSString *bread = [self.breadTypes objectAtIndex:breadTypeRow];
    NSString *filling = [self.fillingTypes objectAtIndex:fillingTypeRow];
    
    NSString *message = [[NSString alloc] initWithFormat:@"Your %@ on %@ bread will be bring up", filling, bread];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Thank you for your order"
                                                    message:message
                                                   delegate:nil
                                          cancelButtonTitle:@"Great!"
                                          otherButtonTitles:nil];
    [alert show];
}


#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}


- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
    if (component == kBreadComponent) {
        return [self.breadTypes count];
    }
    
    return [self.fillingTypes count];
}

#pragma mark -
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row
            forComponent:(NSInteger)component {
    
    if (component == kBreadComponent) {
        return [self.breadTypes objectAtIndex:row];
    }
    
    return [self.fillingTypes objectAtIndex:row];
}

@end
